package cn.gok.server.controller;

import cn.gok.common.config.SystemLog;
import cn.gok.common.entity.goods.GoodsBrand;
import cn.gok.common.resp.PageResult;
import cn.gok.common.resp.Result;
import cn.gok.server.service.GoodsBrandService;
import cn.gok.utils.ResultUtil;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.PostMapping;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RestController;

import javax.servlet.http.HttpServletRequest;
import java.util.ArrayList;
import java.util.List;

/**
 * @author: DxlinY
 * @apiNote: 商品品牌控制层
 * @date: 2020/10/19
 * @time: 17:42
 */
@RestController
@RequestMapping("goodsBrand")
public class GoodsBrandController {


    @Autowired
    private GoodsBrandService goodsBrandService;

    /**
     * 分页
     *
     * @param page  页数
     * @param limit 页容量
     * @return pageResult
     */
    @PostMapping("listAll")
    @SystemLog(model = "商品品牌管理", method = "分页查询")
    public PageResult listAll(int page, int limit, GoodsBrand goodsBrand) {
        return ResultUtil.pageInfo(goodsBrandService.listAll(page, limit, goodsBrand));
    }

    /**
     * 上传
     *
     * @param goodsBrand 品牌对象
     * @return result
     **/
    @PostMapping("insertProductBrand")
    @SystemLog(model = "商品品牌管理", method = "商品品牌上传")
    public Result insertProductBrand(GoodsBrand goodsBrand) {
        if (goodsBrandService.insertBrand(goodsBrand) > 0) {
            return ResultUtil.success(null);
        }
        return ResultUtil.error(null);
    }

    /**
     * 修改
     *
     * @param goodsBrand 品牌对象
     * @return result
     **/
    @PostMapping("updateBrand")
    @SystemLog(model = "商品品牌管理", method = "商品品牌修改")
    public Result updateProductBrandById(GoodsBrand goodsBrand) {
        if (goodsBrandService.updateById(goodsBrand) > 0) {
            return ResultUtil.success(null);
        }
        return ResultUtil.error(null);
    }

    /**
     * 获取单独品牌信息
     *
     * @param id 编号
     * @return result
     **/
    @PostMapping("getById")
    @SystemLog(model = "商品品牌管理", method = "获取品牌信息")
    public Result getById(int id) {
        GoodsBrand brand;
        if ((brand = goodsBrandService.getBrandById(id)) != null) {
            return ResultUtil.success(brand);
        }
        return ResultUtil.error(null);
    }


    /**
     * 批量删除
     *
     * @param request
     * @return result
     */
    @PostMapping("delByIds")
    @SystemLog(model = "商品品牌管理", method = "商品品牌批量删除")
    public Result deleteByIds(HttpServletRequest request) {
        String[] ids = request.getParameterValues("ids");
        List <Integer> list = new ArrayList <>();
        for (String id : ids) {
            list.add(Integer.parseInt(id));
        }
        if (goodsBrandService.deleteByIds(list) > 0) {
            return ResultUtil.success(null);
        }
        return ResultUtil.error(null);
    }
}
